package tech.aistar.corejava.day06;

import java.util.Arrays;

/**
 * Created by 豆梦寒 on 2019/7/22.
 */
public class Emp {
    //定义员工的属性
    private int id;
    private String empName;
    private int age;
    //一个上司对应多个下属
    private Emp[] emps;
    public Emp(){

    }

    public Emp(int id, String empName, int age, Emp[] emps) {
        this.id = id;
        this.empName = empName;
        this.age = age;
        this.emps = emps;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Emp[] getEmps() {
        return emps;
    }

    public void setEmps(Emp[] emps) {
        this.emps = emps;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Emp{");
        sb.append("id=").append(id);
        sb.append(", empName='").append(empName).append('\'');
        sb.append(", age=").append(age);
        sb.append(", emps=").append(Arrays.toString(emps));
        sb.append('}');
        return sb.toString();
    }
    public void addEmp(Emp e){
        if (null == emps) {
            System.out.println("没有权限进行添加");
            return;
        }
        Emp[] emps = new Emp[0];
        emps = Arrays.copyOf(emps,emps.length+1);
        //每次都是在数组的末尾处 - 数组长度 - 1
        emps[emps.length-1] = e;


        System.out.println("有权限添加!");


    }
    public void delById(int id){
        if (null == emps || emps.length == 0) {
            System.out.println("删除失败");
            return;
        }
        //删除之后存放的数组
        Emp[] temp = new Emp[emps.length-1];
        int pos = 0;
        for (int i = 0; i < emps.length; i++) {
            if (emps[i].getId()!=id) {
                temp[pos++]=emps[i];
            }

        }
        emps=temp;

    }
    public void delByEmpName(String empName){
        if (null == emps || emps.length==0 ) {
            System.out.println("删除失败");
            return;
        }
        int count = 0;
        for (int i = 0; i < emps.length; i++) {
            if (emps[i].getEmpName().equals(empName)){
                count++;
            }
        }
        if (count == 0) {
            System.out.println("sorry,不存在");
            return;
        }
        Emp[] temp = new Emp[emps.length - count];

        int pos = 0;

        for (int i = 0; i < emps.length; i++) {
            if (!emps[i].getEmpName().equals(empName)) {
                temp[pos++]=emps[i];
            }
        }
        emps=temp;
    }
}
